var chooseCityType = 0;
var citys = {
	url: "/citys/:type",
	className: "choose_citys",
	render: function() {
		chooseCityType = this.params.type;
		return setTitle("选择城市"), template("tpl_citys", {})
	},
	bind: function() {
		var _this = this;
		var right_index = [];
		ajaxGet("firework/claim-stores/choosecitys.json", {}, function(data) {
			$(_this).find("#citys ul").html(template("tpl_citys_data", data));
			var top = 0;
			$(_this).find("#citys li").each(function(i, ele) {
				var id = "sc_" + i;
				var txt = $(this).data("text");
				var indx = {
					"id": id,
					"text": txt,
					"sctop": top
				};
				top += $(this).height();
				$("#pages").append("<li id='" + id + "'>" + txt + "</li>");
				right_index.push(indx);
			});
			var ph = $("#pages").height();
			var wh = $(window).height();
			$(_this).find("#pages").css("top", (wh - ph) / 2 - 10);
			//绑定点击事件
			$(_this).find("#pages li").click(function() {
				var ind = $(this).index();
				//console.log(ind);
				var matop = right_index[ind]["sctop"] + 5;
				scrollTop3(matop);
				//$('html,body').animate({scrollTop: matop+'px'}, 500);
				//$('html,body').scrollTop(right_index[ind]["sctop"]+5);
			});
			$(window).unbind("scroll").scroll(function() {
				showIndex();
			});
			showIndex();
		})
		var cuIndex = -1;

		function showIndex() {
			var sctop = $(window).scrollTop();
			var selecIndex = 0;
			for(var i = right_index.length; i > 0; i--) {
				if(right_index[i - 1]["sctop"] < sctop) {
					selecIndex = i - 1;
					break;
				}
			}
			if(cuIndex != selecIndex) {
				cuIndex = selecIndex;
				$(_this).find("#pages li").removeClass("cu");
				$(_this).find("#sc_" + selecIndex).addClass("cu");
				$(_this).find("#show_index").html($(_this).find("#citys li").eq(selecIndex).find("p").html());
			}
		}
		//只选择城市
		if(chooseCityType == "name") {
			$(_this).find("#citys").on("click", "a", function() {
				var da = $(this).data();
				setCookie("city", JSON.stringify(da), 7);
				goback(-1);
			})
		} else {
			//选择区县
			$(_this).find("#citys").on("click", "a", function() {
				var da = $(this).data();
				go("#/county/" + da.id);
			})
		}
	}
};
router.push(citys);
var selectCityId = 0;
var countys = {
	url: "/county/:id",
	className: "choose_citys",
	render: function() {
		selectCityId = this.params.id;
		return setTitle("选择地区"), template("tpl_empty", {})
	},
	bind: function() {
		var _this = this;
		$(window).unbind("scroll");
		var city = {};
		ajaxGet("firework/claim-stores/choosecounty/" + selectCityId + ".json", {}, function(data) {
			$(_this).html(template("tpl_county_data", data));
			city = data.cityname;
			$(_this).find("#citys").on("click", "a", function() {
				var da = $(this).data();
				setCookie("city", JSON.stringify(city), 7);
				setCookie("county", JSON.stringify(da), 7);
				goback(-2);
			})
		})
	}
};
router.push(countys);